package com.lxx.onlineview.service;

import com.lxx.onlineview.entity.po.Course;
import com.lxx.onlineview.entity.po.Teach;
import com.lxx.onlineview.results.CodeMsg;
import com.lxx.onlineview.results.Result;
import com.lxx.onlineview.mapper.*;
import com.lxx.onlineview.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.lxx.onlineview.results.CodeMsg.*;


@Service
public class CourseService {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    TeachMapper teachMapper;
    @Autowired
    StudentMapper studentMapper;

    /*public Result<Course> addCourse(Course course, String teacherName){
        int result = courseMapper.insert(course);
        if(result==1){
            result = 0;
            CourseBelong courseBelong = new CourseBelong();
            courseBelong.setCourse(course.getName());
            courseBelong.setCla(className);
            result = courseBelongMapper.insert(courseBelong);
            if(result==1){
                Teach teach = new Teach();
                teach.setCourse(course.getName());
                teach.setTeacher(teacherName);
                result = teachMapper.insert(teach);
                if(result==1){
                    return Result.success(course);
                }
            }
        }
        return Result.error(ADD_FAIL);
    }*/

    public Result<List<Course>> getStudentAllCourse(String studentName){
        List<Course> courses = courseMapper.selectByStudent(studentName);
        if(courses ==null||courses.isEmpty()){
            return Result.error(CodeMsg.NOT_EXIST);
        }else{
            return Result.success(courses);
        }
    }

    public Result<List<Course>> getTeacherAllCourse(String teacherName) {
        List<Course> courses = courseMapper.selectByTeacher(teacherName);
        if(courses ==null||courses.isEmpty()){
            return Result.error(CodeMsg.NOT_EXIST);
        }else{
            return Result.success(courses);
        }
    }

    public Result<String> insert(Course course,String teacherName){
        int result = courseMapper.insert(course);
        result = teachMapper.insert(new Teach(teacherName,course.getName()));
        if(result == 1){
            return Result.success("success");
        }else{
            return Result.error(CodeMsg.ADD_FAIL);
        }
    }

  /*  public Result<String> deleteCourse(String courseName,String teachName){
        int result = courseMapper.deleteByName(courseName);
        if(result==1){
            result = 0;
            result = teachMapper.delete(teachName,courseName);
            if(result==1){
                result = 0;
                result = courseBelongMapper.deleteByCourse(courseName);
                if(result==1){
                    return Result.success("删除成功");
                }
            }
        }
        return Result.error(DELETE_FAIL);
    }*/

    /*public Result<Course> update(Course course,String teacherName,String className){
        //deleteCourse(course.getName(), teacherName);
        Result<Course> resultCourseResult = addCourse(course,teacherName,className);
        return Result.success(resultCourseResult.getData());
    }*/
}


